package com.sc.passbook.advice;

import com.sc.passbook.constant.ErrorInfo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author sc
 * Created on  2018/9/1
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ErrorInfo<String> errorInfo(HttpServletRequest request,Exception e){
        ErrorInfo<String> errorInfo = new ErrorInfo<>();
        errorInfo.setCode(-1);
        errorInfo.setMsg("error");
        errorInfo.setData(e.getMessage());
        errorInfo.setUrl(request.getRemoteAddr());
        return errorInfo;
    }
}
